package com.zmcn.flowindex.business.role.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.zmcn.flowindex.bean.po.Role;
import com.zmcn.flowindex.bean.po.RoleOperate;
import com.zmcn.flowindex.bean.po.SysOperate;
import com.zmcn.flowindex.bean.vo.RoleOperateVO;
import com.zmcn.flowindex.bean.vo.RoleVO;
import com.zmcn.flowindex.business.role.vo.SysOperateExVO;
import com.zmcn.flowindex.fw.action.Page;
import com.zmcn.flowindex.fw.dao.BaseDao;


/**
 * 
 * 角色数据处理层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
public class RoleDao extends BaseDao {
	/**
	 * 获取角色列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page<RoleVO> getList(Page<RoleVO> page) {
		String hqlResult = "select new " + RoleVO.class.getName() + RoleVO.STRUCT;
		String hqlCount = "select count(role)";
		StringBuffer sb = new StringBuffer();
		sb.append(" from ").append(Role.class.getName()).append(" role");
		sb.append(" where role.status=0");
		List<Object> ps = new ArrayList<Object>();
		for (Iterator<String> item = page.getParas().keySet().iterator(); item.hasNext();) {
			String key = item.next();
			if ("name".equals(key) || "description".equals(key)) {
				sb.append(" and role.").append(key).append(" like ?");
				ps.add("%" + page.getParas().get(key)[0] + "%");
			}
		}
		// do count
		Query qCount = getSession().createQuery(hqlCount + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			qCount.setParameter(i, ps.get(i));
		}
		Long count = (Long) qCount.uniqueResult();
		page.setCount(count);
		// do result
		Query q = getSession().createQuery(hqlResult + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			q.setParameter(i, ps.get(i));
		}
		q.setFirstResult(page.getFirst());
		q.setMaxResults(page.getPageSize());
		page.setResult(q.list());
		return page;
	}

	/**
	 * 获取空白模块分组操作
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SysOperateExVO> getOps() {
		StringBuffer sb = new StringBuffer();
		sb.append("select new ").append(SysOperateExVO.class.getName()).append(SysOperateExVO.STRUCT);
		sb.append(" from ").append(SysOperate.class.getName()).append(" sysoperate");
		sb.append(" order by sysoperate.orderKey");
		Query q = this.getSession().createQuery(sb.toString());
		List<SysOperateExVO> tmp = (List<SysOperateExVO>) q.list();
		return tmp;
	}

	/**
	 * 获取空白模块分组操作
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<RoleOperateVO> getSysOps(String roleId) {
		StringBuffer sb = new StringBuffer();
		sb.append("select new ").append(RoleOperateVO.class.getName()).append(RoleOperateVO.STRUCT);
		sb.append(" from ").append(RoleOperate.class.getName()).append(" roleoperate");
		sb.append(" where roleoperate.roleId=?");
		Query q = this.getSession().createQuery(sb.toString());
		q.setParameter(0, roleId);
		List<RoleOperateVO> tmp = (List<RoleOperateVO>) q.list();
		return tmp;
	}
	
	/**
	 * 根据角色ID获取角色信息
	 * 
	 * @param roleId
	 *            角色ID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public RoleVO getRole(String roleId) {
		RoleVO result = null;
		StringBuffer sb = new StringBuffer();
		sb.append("select new ").append(RoleVO.class.getName()).append(RoleVO.STRUCT);
		sb.append(" from ").append(Role.class.getName()).append(" role");
		sb.append(" where role.roleId=?");
		Query q=this.getSession().createQuery(sb.toString());
		q.setParameter(0, roleId);
		List<RoleVO> tmpList = (List<RoleVO>) q.list();
		if (tmpList.size() > 0) {
			result = tmpList.get(0);
		}
		return result;
	}
}
